"""migrate conversation table

Revision ID: 55f5817d7b97
Revises: 9db9c74ab460
Create Date: 2024-06-26 09:56:06.005120

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '55f5817d7b97'
down_revision: Union[str, None] = '9db9c74ab460'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('chats',
                    sa.Column('id', sa.Integer(),
                              autoincrement=True, nullable=False),
                    sa.Column('bot_id', sa.Integer(), nullable=False),
                    sa.Column('bot_config_id', sa.Integer(), nullable=False),
                    sa.Column('created_by', sa.Integer(), nullable=True),
                    sa.Column('created_at', sa.DateTime(), nullable=False),
                    sa.Column('updated_at', sa.DateTime(), nullable=False),
                    sa.ForeignKeyConstraint(
                        ['bot_config_id'], ['agent_configs.id'], ),
                    sa.ForeignKeyConstraint(['bot_id'], ['agent_bots.id'], ),
                    sa.PrimaryKeyConstraint('id')
                    )
    op.add_column('messages', sa.Column(
        'chat_id', sa.Integer(), nullable=False))
    op.add_column('messages', sa.Column(
        'chat_trun_id', sa.UUID(), nullable=False))
    op.alter_column('messages', 'message_type',
                    existing_type=sa.Enum(
                        'MARKDOWN', 'TEXT', name='messagetype'),
                    type_=sa.VARCHAR(length=50),
                    existing_nullable=False)
    op.drop_column('messages', 'conversation_id')
    op.drop_table('conversations')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('messages', sa.Column('conversation_id',
                  sa.INTEGER(), autoincrement=False, nullable=False))
    op.alter_column('messages', 'message_type',
                    existing_type=sa.Enum(
                        'MARKDOWN', 'TEXT', name='messagetype'),
                    type_=sa.VARCHAR(length=50),
                    existing_nullable=False)
    op.drop_column('messages', 'chat_trun_id')
    op.drop_column('messages', 'chat_id')
    op.create_table('conversations',
                    sa.Column('id', sa.INTEGER(),
                              autoincrement=True, nullable=False),
                    sa.Column('bot_id', sa.INTEGER(),
                              autoincrement=False, nullable=False),
                    sa.Column('bot_config_id', sa.INTEGER(),
                              autoincrement=False, nullable=False),
                    sa.Column('created_at', postgresql.TIMESTAMP(),
                              autoincrement=False, nullable=False),
                    sa.Column('updated_at', postgresql.TIMESTAMP(),
                              autoincrement=False, nullable=False),
                    sa.Column('created_by', sa.INTEGER(),
                              autoincrement=False, nullable=True),
                    sa.ForeignKeyConstraint(['bot_config_id'], [
                        'agent_configs.id'], name='conversations_bot_config_id_fkey'),
                    sa.ForeignKeyConstraint(
                        ['bot_id'], ['agent_bots.id'], name='conversations_bot_id_fkey'),
                    sa.PrimaryKeyConstraint('id', name='conversations_pkey')
                    )
    op.drop_table('chats')
    # ### end Alembic commands ###
